﻿package com.zzvcom.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import com.mortennobel.imagescaling.ResampleOp;
import org.apache.log4j.Logger;

/**
 * 图片缩放工具类
 * 
 * @author sunnymoon
 */
public class ImageResizer
{
	private final Logger log = Logger.getLogger(this.getClass());

	/**
	 * 接收输入流输生成图片
	 * 
	 * @param input
	 * @param writePath
	 * @param width
	 * @param height
	 * @param format
	 * @return
	 */
	public boolean resizeImage(InputStream input, String writePath,
			Integer width, Integer height, String format)
	{
		try
		{
			BufferedImage inputBufImage = ImageIO.read(input);
			log.info("转前图片高度和宽度：" + inputBufImage.getHeight() + ":"
					+ inputBufImage.getWidth());
			ResampleOp resampleOp = new ResampleOp(width, height);// 转换
			BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,
					null);
			ImageIO.write(rescaledTomato, format, new File(writePath));
			log.info("转后图片高度和宽度：" + rescaledTomato.getHeight() + ":"
					+ rescaledTomato.getWidth());
			return true;
		} catch (IOException e)
		{
			e.printStackTrace();
			return false;
		}

	}

	/**
	 * 接收File输出图片
	 * 
	 * @param file
	 * @param writePath
	 * @param width
	 * @param height
	 * @param format
	 * @return
	 */
	public boolean resizeImage(File file, String writePath, Integer width,
			Integer height, String format)
	{
		try
		{
			BufferedImage inputBufImage = ImageIO.read(file);
			inputBufImage.getType();
			log.info("转前图片高度和宽度：" + inputBufImage.getHeight() + ":"
					+ inputBufImage.getWidth());
			ResampleOp resampleOp = new ResampleOp(width, height);// 转换
			BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,
					null);
			ImageIO.write(rescaledTomato, format, new File(writePath));
			log.info("转后图片高度和宽度：" + rescaledTomato.getHeight() + ":"
					+ rescaledTomato.getWidth());
			return true;
		} catch (IOException e)
		{
			e.printStackTrace();
			return false;
		}

	}
	/**
	 * 固定宽度等比缩放
	 * @param file
	 * @param width
	 * @param height
	 * @param format
	 * @return
	 */
	public boolean resizeImageWidth(File file,String writePath, Integer width, String format)
	{
		try
		{
			BufferedImage inputBufImage = ImageIO.read(file);
			inputBufImage.getType();
			log.info("转前图片高度和宽度：" + inputBufImage.getHeight() + ":"
					+ inputBufImage.getWidth());
			Integer height=inputBufImage.getHeight()*width/inputBufImage.getWidth();
			ResampleOp resampleOp = new ResampleOp(width, height);// 转换
			BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,
					null);
			ImageIO.write(rescaledTomato, format, new File(writePath));
			log.info("转后图片高度和宽度：" + rescaledTomato.getHeight() + ":"
					+ rescaledTomato.getWidth());
			return true;
		} catch (IOException e)
		{
			e.printStackTrace();
			return false;
		}

	}	

	/**
	 * 接收字节数组生成图片
	 * 
	 * @param RGBS
	 * @param writePath
	 * @param width
	 * @param height
	 * @param format
	 * @return
	 */
	public boolean resizeImage(byte[] RGBS, String writePath, Integer width,
			Integer height, String format)
	{
		InputStream input = new ByteArrayInputStream(RGBS);
		return this.resizeImage(input, writePath, width, height, format);
	}

	public byte[] readBytesFromIS(InputStream is) throws IOException
	{
		int total = is.available();
		byte[] bs = new byte[total];
		is.read(bs);
		return bs;
	}

	// 测试：只测试了字节流的方式，其它的相对简单，没有一一测试
	public static void main(String[] args) throws IOException
	{

		Integer width = 150;		
		File inputFile = new File("F:\\需翻译.JPG");
		File outFile = new File("F:\\to.jpg");
		String outPath = outFile.getAbsolutePath();
		ImageResizer myImage = new ImageResizer();	
		myImage.resizeImageWidth(inputFile, outPath, width, "jpg");
	}
}
